package com.imooc.service.impl;

import com.imooc.base.BaseInfoProperties;
import com.imooc.mo.HrCollectResumeMO;
import com.imooc.repository.HrCollectResumeRepository;
import com.imooc.service.CountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author liansen
 * @create 06-20-0:02
 */
@Service
public class CountsServicesImpl extends BaseInfoProperties implements CountsService {

    @Autowired
    private HrCollectResumeRepository collectRepository;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void addCollect(String hrId, String resumeExpectId) {
        HrCollectResumeMO collectResumeMO = new HrCollectResumeMO();
        collectResumeMO.setHrId(hrId);
        collectResumeMO.setResumeExpectId(resumeExpectId);
        collectResumeMO.setCreateTime(LocalDateTime.now());
        collectRepository.save(collectResumeMO);

        //收藏简历后，hr收藏数量加1
        redisTemplate.opsForValue().increment(HR_COLLECT_RESUME_COUNTS + ":" + hrId, 1);

    }

    @Override
    public void removeCollect(String hrId, String resumeExpectId) {
        collectRepository.deleteByHrIdAndResumeExpectId(hrId, resumeExpectId);
        //移除收藏的简历后，收藏数量减1
        redisTemplate.opsForValue().decrement(HR_COLLECT_RESUME_COUNTS + ":" + hrId, 1);
    }

    @Override
    public boolean isHrCollectResume(String hrId, String resumeExpectId) {
        List<HrCollectResumeMO> list = collectRepository.findByHrIdAndResumeExpectId(hrId, resumeExpectId);
        if(CollectionUtils.isEmpty(list)){
            return false;
        }
        return true;
    }

    @Override
    public List<HrCollectResumeMO> queryPagedCollectResumeList(String hrId, Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page, pageSize, Sort.Direction.DESC, "create_time");
        List<HrCollectResumeMO> list = collectRepository.findAllByHrIdOrderByCreateTimeDesc(hrId, pageable);
        return list;
    }
}
